package com.ruoyi.form.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.Md5Utils;
import com.ruoyi.form.domain.FormRegisterVo;
import com.ruoyi.form.dto.ExcelDtoMap;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.form.mapper.FormRegisterMapper;
import com.ruoyi.form.domain.FormRegister;
import com.ruoyi.form.service.IFormRegisterService;
import com.ruoyi.common.core.text.Convert;

/**
 * 进件+申请接口Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-11-18
 */
@Service
public class FormRegisterServiceImpl implements IFormRegisterService 
{
    @Autowired
    private FormRegisterMapper formRegisterMapper;

    /**
     * 查询进件+申请接口
     * 
     * @param id 进件+申请接口主键
     * @return 进件+申请接口
     */
    @Override
    public FormRegister selectFormRegisterById(Integer id)
    {
        return formRegisterMapper.selectFormRegisterById(id);
    }

    /**
     * 查询进件+申请接口列表
     * 
     * @param formRegister 进件+申请接口
     * @return 进件+申请接口
     */
    @Override
    public List<FormRegister> selectFormRegisterList(FormRegister formRegister)
    {
        return formRegisterMapper.selectFormRegisterList(formRegister);
    }

    @Override
    public List<FormRegister> selectFormRegisterList2(FormRegister formRegister)
    {
        return formRegisterMapper.selectFormRegisterList2(formRegister);
    }

    @Override
    public List<FormRegister> selectFormRegisterList1(FormRegister formRegister)
    {
        return formRegisterMapper.selectFormRegisterList1(formRegister);
    }

    /**
     * 新增进件+申请接口
     * 
     * @param formRegister 进件+申请接口
     * @return 结果
     */
    @Override
    public int insertFormRegister(FormRegister formRegister)
    {
        formRegister.setCreateTime(DateUtils.getNowDate());
        return formRegisterMapper.insertFormRegister(formRegister);
    }

    /**
     * 修改进件+申请接口
     * 
     * @param formRegister 进件+申请接口
     * @return 结果
     */
    @Override
    public int updateFormRegister(FormRegister formRegister)
    {
        formRegister.setUpdateTime(DateUtils.getNowDate());
        return formRegisterMapper.updateFormRegister(formRegister);
    }

    /**
     * 批量删除进件+申请接口
     * 
     * @param ids 需要删除的进件+申请接口主键
     * @return 结果
     */
    @Override
    public int deleteFormRegisterByIds(String ids)
    {
        return formRegisterMapper.deleteFormRegisterByIds(Convert.toStrArray(ids));
    }

    @Override
    public int reSend(String ids)
    {
        return formRegisterMapper.reSend(Convert.toStrArray(ids));
    }

    /**
     * 删除进件+申请接口信息
     * 
     * @param id 进件+申请接口主键
     * @return 结果
     */
    @Override
    public int deleteFormRegisterById(Integer id)
    {
        return formRegisterMapper.deleteFormRegisterById(id);
    }

    @Override
    public String importDataV2(List<FormRegisterVo> list1, Boolean isUpdateSupport, String operName, Long userId)
    {
        if (StringUtils.isNull(list1) || list1.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (FormRegisterVo info : list1)
        {
//            try
//            {
                FormRegister register = new FormRegister();
                register.setSex(ExcelDtoMap.getValue(info.getSex()));
                register.setMonthlyIncome(ExcelDtoMap.getValue(info.getMonthlyIncome()));
                register.setQualification(ExcelDtoMap.getValue(info.getQualification()));
                register.setProfession(ExcelDtoMap.getValue(info.getProfession()));
                register.setGjj(ExcelDtoMap.getValue(info.getGjj()));
                register.setSocialInsurance(ExcelDtoMap.getValue(info.getSocialInsurance()));
                register.setHouse(ExcelDtoMap.getValue(info.getHouse()));
                register.setCar(ExcelDtoMap.getValue(info.getCar()));
                register.setSesame(ExcelDtoMap.getValue(info.getSesame()));
                register.setInsurancePolicy(ExcelDtoMap.getValue(info.getInsurancePolicy()));
                register.setPayType(ExcelDtoMap.getValue(info.getPayType()));
                register.setCreditCard(ExcelDtoMap.getValue(info.getCreditCard()));
                register.setCreditInformation(ExcelDtoMap.getValue(info.getCreditInformation()));

                register.setCity(info.getCity());
                register.setProvince(info.getProvince());
                register.setIpAddress(info.getIpAddress());
                register.setUserPhone(info.getUserPhone());
                register.setIdCard(info.getIdCard());
                register.setName(info.getName());
                register.setAge(info.getAge());
                register.setLoanAmount(info.getLoanAmount());
                register.setLoanTerm(info.getLoanTerm());
                register.setUserPhoneMd5(Md5Utils.hash(info.getUserPhone()));
                register.setCreateTime(new Date());
                register.setUpdateTime(new Date());
                register.setState(1);
                register.setUserId(userId);
                formRegisterMapper.insertFormRegister(register);
            }
//            catch (Exception e)
//            {
//                failureMsg.append("<br/>" + " 导入失败，请联系管理员!");
//                throw new ServiceException(e.getMessage());
//            }
//        }
        successMsg.append("<br/>" + " 导入成功!");
        return successMsg.toString();
    }
}
